package com.czzapp.xiaoyumemo.view;


import java.io.File;

import com.czzapp.xiaoyumemo.R;
import com.czzapp.xiaoyumemo.model.MemoManager;
import com.czzapp.xiaoyumemo.model.SdStatus;
import com.czzapp.xiaoyumemo.model.SdStatusManager;
import com.guohead.sdk.GuoheAdManager;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

import android.widget.RadioGroup.OnCheckedChangeListener;

//主界面
public class XiaoyuMemoActivity extends TabActivity implements OnCheckedChangeListener{
    /** Called when the activity is first created. */

	private RadioGroup mainTab;
	private TabHost mTabHost;
	//内容Intent
		private Intent mCurrentIntent;
		private Intent mAgoIntent;
		private Intent mCalendarIntent;
		
		private final static String TAB_TAG_CUR="tab_tag_cur";
		private final static String TAB_TAG_AGO="tab_tag_ago";
		private final static String TAB_TAG_CALENDAR="tab_tag_calendar";

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        if(SdStatusManager.getInstance().getSdStatus() == SdStatus.Init) {
        	SdStatusManager.getInstance().setSdStatus(SdStatus.Umounted);
        	if(Environment.getExternalStorageState().compareTo(Environment.MEDIA_MOUNTED) == 0) {
        		SdStatusManager.getInstance().setSdStatus(SdStatus.Preparing);
        		File sdFile = Environment.getExternalStorageDirectory();
        		if(sdFile.canWrite()) {
			        MemoManager.init(this);
	        		SdStatusManager.getInstance().setSdStatus(SdStatus.Ready);
        		}
        	}
        }
        SdStatus sdStatus = SdStatusManager.getInstance().getSdStatus();
        if(sdStatus != SdStatus.Ready) {
        	String message = null;
        	if(sdStatus == SdStatus.Umounted) {
        		message = "请插入sd卡";
        	}
        	else if(sdStatus == SdStatus.Preparing) {
        		message = "正在准备sd卡";
        	}
        	AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        	builder.setMessage(message) 
        	       .setCancelable(false) 
        	       .setPositiveButton("确定", new DialogInterface.OnClickListener() { 
        	           public void onClick(DialogInterface dialog, int id) { 
        	                XiaoyuMemoActivity.this.finish(); 
        	           } 
        	       }).show() ;
        }
        else
        {
        	mainTab=(RadioGroup)findViewById(R.id.main_tab);
            mainTab.setOnCheckedChangeListener(this);
            prepareIntent();
            setupIntent();
            RadioButton raido = (RadioButton)findViewById(R.id.radio_button0);
            raido.setChecked(true);
			//frameLayout = mTabHost.getTabContentView();
        }
        
       
//		gestureDetector = new GestureDetector(new TabHostTouch());
//		new View.OnTouchListener() {
//			public boolean onTouch(View v, MotionEvent event) {
//				if (gestureDetector.onTouchEvent(event)) {
//					return true;
//				}
//				return false;
//			}
//		};

    }
    /**
     * 准备tab的内容Intent
     */
	private void prepareIntent() {
		mCurrentIntent=new Intent(this, CurrentMemoTabActivity.class);
		mAgoIntent=new Intent(this, AgoMemoTabActivity.class);
		mCalendarIntent=new Intent(this, CalendarTabActivity.class);

	}
	/**
	 * 
	 */
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) {
		return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),
				getResources().getDrawable(resIcon)).setContent(content);
	} 
	
	private void setupIntent() {
		this.mTabHost=getTabHost();
		TabHost localTabHost=this.mTabHost;
		localTabHost.addTab(buildTabSpec(TAB_TAG_CUR,R.string.main_cur, R.drawable.memo_btn_bg, mCurrentIntent));
		localTabHost.addTab(buildTabSpec(TAB_TAG_AGO, R.string.main_ago, R.drawable.memo_btn_bg, mAgoIntent));
		localTabHost.addTab(buildTabSpec(TAB_TAG_CALENDAR, R.string.main_calendar, R.drawable.memo_btn_bg, mCalendarIntent));
	}
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch(checkedId){
		case R.id.radio_button0:
			this.mTabHost.setCurrentTabByTag(TAB_TAG_CUR);
			break;
		case R.id.radio_button1:
			this.mTabHost.setCurrentTabByTag(TAB_TAG_AGO);
			break;
		case R.id.radio_button2:
			this.mTabHost.setCurrentTabByTag(TAB_TAG_CALENDAR);
			break;
		}	    
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		GuoheAdManager.finish(this);
	}
	

    
}